package operation.android.util;

import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 读写文件工具类
 */
public class UpdateUtil {
    /**
     * 读取文件，用于文件回显到页面
     *
     * @param url 文件路径 + 文件名
     * @return string 属性
     */
    public static Map<Integer, String> readFile(String url) {
        BufferedReader br = null;
        String file = "";
        try {
            br = new BufferedReader(new FileReader(url));    // 读取文件

            String line = null;
            while ((line = br.readLine()) != null) {    // 按行读取
                if (StringUtils.isNotBlank(line)) {
                    file += line + "#";
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        Map<Integer, String> map = new HashMap<>();
        String[] split = file.split("#");
        for (int line = 0; line < split.length; line++) {
            map.put(line, split[line]);
        }
        return map;
    }

    /**
     * 更新文件
     *
     * @param url     文件路径，用 / 结尾
     * @param content 修改追加的文件内容，或者修改的文件内容，用 ; 分割
     */
    public static void updateFile(String url, String content) {

        BufferedReader br = null;
        BufferedWriter out = null;

        try {
            br = new BufferedReader(new FileReader(url));    // 读取文件
            out = new BufferedWriter(new FileWriter(url));   // 备份文件写入

            String[] split = content.split("#");    // 处理需要写入的新数据
            Map<String, String> map = new HashMap<>();  // 保存新数据
            for (int line = 0; line < split.length; line++) {
                map.put(String.valueOf(line), split[line]);
            }

            String line = null;
            while ((line = br.readLine()) != null) {    // 按行读取
                out.write(line); // 写入备份文件，换行写入
                out.newLine();
            }
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();

        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 测试
     */
    public static void main(String args[]) {
        Map<Integer, String> getMap = new HashMap<>();
        System.out.println("开始读取页面代码");
//        // 读取文件
        BufferedReader br = null;
        String file = "";
        String url = "D:\\workspace\\onego\\src\\main\\java\\com\\example\\onego\\android\\out\\updateTest.java";
        try {
            br = new BufferedReader(new FileReader(url));    // 读取文件

            String line = null;
            while ((line = br.readLine()) != null) {// 按行读取
                if (line.contains("throw new IllegalArgumentException(\"Unknown ViewModel class: \" + modelClass.getName());")) {
                    break;
                } else {
                    file += line + "#";
                }
            }
            System.out.println("1");
            String ClassName = "HelloWorld";
            String projectName = "测试功能";
            System.out.println("2");
            String[] split = file.split("#");
            for (int line1 = 0; line1 < split.length - 1; line1++) {
                getMap.put(line1, split[line1]);
            }
            getMap.put(split.length - 1, "        }else if (modelClass.isAssignableFrom(" + ClassName + "ViewModel.class)) { // " + projectName);
            getMap.put(split.length, "            return (T) new " + ClassName + "ViewModel(mApplication, mRepository);");
            getMap.put(split.length + 1, "        }");
            getMap.put(split.length + 2, "        throw new IllegalArgumentException(\"Unknown ViewModel class: \" + modelClass.getName());");
            getMap.put(split.length + 3, "    }");
            getMap.put(split.length + 4, "}");


            for (int i = 0; i < getMap.size(); i++) {
                System.out.println(getMap.get(i));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


        // 修改文件

        File outfile = new File(url);
        String info = "";
        // 输入文件
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(outfile);
            for (int i = 0; i < getMap.size(); i++) {
                info = System.getProperty("line.separator") + getMap.get(i);
                fileWriter.write(info);
            }
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        for (int i = 0; i < getMap.size(); i++) {
//            try {
//                fileWriter.write(getMap.get(i) + "\n");
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
    }
}
